#pragma once
#include <iostream>
#include <pthread.h>
#include <string>
#include <functional>
#include "logmessage.hpp"

namespace zty
{
    typedef void *(*func_t)(void *);

    class thread
    {
        pthread_t _tid;
        func_t _callback = nullptr;
        void *_args = nullptr;

    public:
        thread() {}

        thread(func_t callback, void *args)
            : _callback(callback),
              _args(args)
        {
            pthread_create(&_tid, nullptr, _callback, _args);
        }

        thread(const thread &other) = delete;
        thread &operator=(const thread &other) = delete;

        void run(func_t callback, void *args)
        {
            _callback = callback;
            _args = args;
            pthread_create(&_tid, nullptr, _callback, _args);
            // printf("[%d] run\n", _tid%10000);
            LogMessage(DEBUG, "[%d] run", _tid%10000);
        }

        void join()
        {
            // printf("[%d] join\n", _tid%10000);
            LogMessage(DEBUG, "[%d] join", _tid%10000);
            pthread_join(_tid, nullptr);
        }

        pthread_t get_id()
        {
            return _tid;
        }
    };
}